﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TIBCO.EMS;
using System.Collections;

namespace TibcoMessageAdmin
{
    public partial class frmResend : Form
    {
        public string XmlMessage ;
        public string MessageID;

        public frmResend()
        {
            InitializeComponent();
        }

        

        private void cmdSend_Click(object sender, EventArgs e)
        {
            
            if (SendMessage(XmlMessage, txtServer.Text, txtUser.Text, txtPwd.Text, txtQueue.Text))
            {
                MessageBox.Show("Message Sent successfully"); 

            }


            
            

                

        }

        public bool SendMessage(string messageToSend, string serverName,string user, string password ,string queueName)
        {
            QueueConnectionFactory factory = null;
            QueueConnection connection = null;
            QueueSession session = null;
            bool status = false;
            TIBCO.EMS.Queue queue = null;            
            TextMessage msg = null;
            QueueSender sender = null;

            try
            {





                factory = new TIBCO.EMS.QueueConnectionFactory(serverName);
                connection = factory.CreateQueueConnection(user, password);
                session = connection.CreateQueueSession(false, Session.AUTO_ACKNOWLEDGE);
                queue = session.CreateQueue(queueName);
                sender = session.CreateSender(queue);
                msg = new TextMessage(session);
                msg.Text = messageToSend;
                sender.Send(msg);
                status = true;
            }
            catch (Exception ex)
            {
                //TODO : Log the exception 

            }
            finally
            {

                sender.Close();
                session.Close();
                connection.Close();

            }
            return status;
        }

    }
}
